package com.haitai.gas.integration.poi.policy;

import com.deepoove.poi.policy.AbstractRenderPolicy;
import com.deepoove.poi.render.RenderContext;
import com.deepoove.poi.util.ParagraphUtils;
import com.deepoove.poi.xwpf.BodyContainer;
import com.deepoove.poi.xwpf.BodyContainerFactory;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;

import java.util.List;

public abstract class BaseRenderPolicy<T>  extends AbstractRenderPolicy<T> {

    public static final int PAGE_INDEX = 1;
    public static final int PAGE_STATEMENT = 2;
    public static final int PAGE_SURVEY = 3;
    public static final int PAGE_CONTENT = 4;
    public static final int PAGE_EVALUATE = 5;

    public int getIndexOfFun(RenderContext renderContext){
        XWPFRun run = renderContext.getWhere();
        IRunBody parent = run.getParent();
        run.setText("", 0);
        if (parent instanceof XWPFParagraph) {
            String paragraphText = ParagraphUtils.trimLine((XWPFParagraph)parent);
            boolean havePictures = ParagraphUtils.havePictures((XWPFParagraph)parent);
            boolean havePageBreak = ParagraphUtils.havePageBreak((XWPFParagraph)parent);
            boolean haveObject = ParagraphUtils.haveObject((XWPFParagraph)parent);
            if ("".equals(paragraphText) && !havePictures && !havePageBreak && !haveObject) {
                int pos = this.getPosOfParagraph((XWPFParagraph)parent, run);
//                this.removeBodyElement(pos);
                return pos;
            }
        }
        return -1;
    }

    int getPosOfParagraph(XWPFParagraph paragraph, XWPFRun run) {
        return this.getPosOfParagraphCTP(paragraph.getCTP(),run);
    }

    int getPosOfParagraphCTP(CTP ctp, XWPFRun run) {
        BodyContainer bodyContainer = BodyContainerFactory.getBodyContainer(run);
        List<IBodyElement> bodyElements = bodyContainer.getBodyElements();

        for(int i = 0; i < bodyElements.size(); ++i) {
            IBodyElement current = (IBodyElement)bodyElements.get(i);
            if (current.getElementType() == BodyElementType.PARAGRAPH && ((XWPFParagraph)current).getCTP().equals(ctp)) {
                return i;
            }
        }

        return -1;
    }

    public void doRender(RenderContext<T> context){
        XWPFDocument xwpfDocument = context.getXWPFDocument();
//        xwpfDocument.removeBodyElement(0);
        clearPlaceholder(context, true);
//        if(getPage() != PAGE_INDEX) {
            newPage(xwpfDocument);
//        }
        doRender(context, xwpfDocument);
    }

    public abstract int getPage();

    public abstract void doRender(RenderContext<T> context, XWPFDocument xwpfDocument);

    protected void addBreak(XWPFDocument xwpfDocument, int size) {
        XWPFParagraph paragraph = xwpfDocument.createParagraph();
        paragraph.setAlignment(ParagraphAlignment.CENTER);
        XWPFRun run = paragraph.createRun();
        run.setFontSize(8);
        for (int i = 0; i < size; i++) {
//            run.addBreak();
            run.addCarriageReturn();
        }
    }

    protected void newPage(XWPFDocument xwpfDocument) {
        xwpfDocument.createParagraph().createRun().addBreak(BreakType.PAGE);
    }
}
